package com.weetop.cfw.base.presenter.imp

import com.weetop.cfw.base.presenter.PasswordLogInPresenter
import com.weetop.cfw.base.ui.activity.CommonBaseActivity
import com.weetop.cfw.base.view.BaseView
import com.weetop.cfw.base.view.PasswordLogInView
import com.weetop.cfw.bean.LogInBean
import com.weetop.cfw.callback.RxJavaCallBack
import com.weetop.cfw.log_in.PasswordLogInActivity
import com.weetop.cfw.utils.RetrofitUtils
import com.weetop.cfw.utils.RxJavaUtils

class PasswordLogInPresenterImp(var passwordLogInView: PasswordLogInView? = null):PasswordLogInPresenter {


    /**
     * 密码登录
     * @param username 用户名
     * @param userpwd 密码
     */
    override fun passwordLogIn(passwordLogInActivity: CommonBaseActivity, username: String, userpwd: String) {
        when {
            username.isEmpty() -> {
                passwordLogInView?.showNativeShortToast("请输入用户名")
            }
            userpwd.isEmpty() -> {
                passwordLogInView?.showNativeShortToast("请输入密码")
            }
            else -> {
                passwordLogInActivity.showLoadingDialog(passwordLogInActivity,"正在登录...")
                RxJavaUtils.useInAppCompatActivityOnDestory(RetrofitUtils.getApi().passwordLogIn(username,userpwd),passwordLogInActivity,object:RxJavaCallBack<LogInBean>(){
                    override fun onSuccess(t: LogInBean?) {
                        passwordLogInView?.logInSuccess(t)
                        passwordLogInActivity.dismissLoadingDialog()
                    }

                    override fun onRequestError(t: LogInBean?) {
                        super.onRequestError(t)
                        passwordLogInActivity.dismissLoadingDialog()
                    }

                    override fun onErrorResponse(throwable: Throwable?) {
                        super.onErrorResponse(throwable)
                        passwordLogInActivity.dismissLoadingDialog()
                    }

                })
            }
        }
    }


    /**
     * 绑定
     */
    override fun attachView(baseView: BaseView) {
        passwordLogInView = baseView as? PasswordLogInView
    }

    /**
     * 解绑
     */
    override fun detachView() {
        passwordLogInView = null
    }
}